package com.iyao_app.http;

import java.util.HashMap;
import java.util.Iterator;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/12/13.
 */

public class ShareUrl {
    private ShareUrl() {

    }

    private static ShareUrl shareUrl;

    public static ShareUrl getInstace() {
        if (shareUrl == null) {
            shareUrl = new ShareUrl();
        }
        return shareUrl;
    }

    /**
     * 分享故事墙
     *
     * @param eventId
     * @return
     */
    public String shareStory(int eventId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("eventId", eventId);
        return getUrl(RequestUrl.SHARE_STORY, params);
    }

    /**
     * 签到
     *
     * @param sceneId
     * @return
     */
    public String signShare(int sceneId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("sceneId", sceneId);
        return getUrl(RequestUrl.SHARE_SIGN, params);
    }

    /**
     * 我的签到
     *
     * @param userId
     * @return
     */
    public String mySignShare(String userId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        return getUrl(RequestUrl.SHARE_MY_SIGN, params);
    }

    private String getUrl(String url, final HashMap<String, Object> params) {
        // 添加url参数
        if (params != null) {
            Iterator<String> it = params.keySet().iterator();
            StringBuffer sb = null;
            while (it.hasNext()) {
                String key = it.next();
                String value = String.valueOf(params.get(key));
                if (sb == null) {
                    sb = new StringBuffer();
                    sb.append("?");
                } else {
                    sb.append("&");
                }
                sb.append(key);
                sb.append("=");
                sb.append(value);
            }
            if (sb != null)
                url += sb.toString();
        }
        return url;
    }
}
